package com.aaa.book_mall.dao.impl;

import com.aaa.book_mall.dao.DepartmentDao;
import com.aaa.book_mall.entity.Department;

import com.aaa.book_mall.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public  class DepartmentDaoImpl implements DepartmentDao {
    @Override
    public List<Map<String, Object>> getAllDempartment(Department department) {
        System.out.println("-----------------------");
        //StringBuilder sql = new StringBuilder("SELECT td.deptid,td.deptNo,td.dname,count(*) ,td.`status`,td.remark  FROM t_dept td JOIN t_staff ts ON td.deptid=ts.deptid  ");
        StringBuilder sql=new StringBuilder("  select a.deptid,deptNo,dname,IFNULL(b.num,0)as num ,status,remark from t_dept as a\n" +
                "LEFT JOIN(SELECT deptid,COUNT(*)AS num from t_staff GROUP BY deptid)as b\n" +
                "ON a.deptid=b.deptid");
        //判断此时的搜索框不能为null和空字符创时才进行拼接
        if(department.getDname()!=null && !"".equals(department.getDname())){
            sql.append(" WHERE dname LIKE '%"+department.getDname()+"%'");
        }
        //sql.append(" GROUP BY dname ORDER BY deptid LIMIT ?,?");
        sql.append("  LIMIT ?,?");
        //System.out.println(sql);
        List<Map<String,Object>> maps = JDBCUtil.queryListMap(sql.toString(),(department.getPage()-1)*department.getLimit(),department.getLimit());
        System.out.println(maps);
        if(maps.size()>0){
            return maps;
        }
        return null;
    }

    @Override
    public int getBookCount(Department department) {
        StringBuilder sql = new StringBuilder("select count(*) from t_dept");
        if(department.getDname()!=null && !"".equals(department.getDname())){
            sql.append(" WHERE dname  LIKE '%"+department.getDname()+"%'");
        }
        //System.out.println(sql);
        List<List> lists = JDBCUtil.queryList(sql.toString());
        if(lists.size()>0){
            //因为是List<List>，所有两层数据，一层一层的获取，所以写两个get(0)
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }

    @Override
    public int delDeptidById(Integer deptid) {
        String sql="delete from t_dept where deptid=?";
        int i = JDBCUtil.update(sql, deptid);
        return i;
    }

    @Override
    public int insertDepartment(Department department) {
        System.out.println("----增加部门");
        String sql="insert into t_dept values(null,?,?,?,?)";
        int i = JDBCUtil.update(sql, department.getDeptNo(), department.getDname(), department.getRemark(),1);

        return i;
    }

    @Override
    public int updateDepartment(Department department) {
        System.out.println("修改部门");
        String sql="update t_dept set deptNo=?,dname=?,remark=? where deptid=?";
        int i = JDBCUtil.update(sql, department.getDeptNo(), department.getDname(), department.getRemark(), department.getDeptid());
        return i;
    }

    @Override
    public int updateStatus(Integer deptid, Integer status) {
        System.out.println("-----");

        String sql = "update t_dept set status=? where deptid=?";
        int i = 0;
        if(status==1){
            i = JDBCUtil.update(sql, 0, deptid);
        }else{
            i = JDBCUtil.update(sql,1,deptid);
        }
        return i;
    }
}
